package mo.tags.stack;

import mo.common.TreeNode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BSTIterator {

    private List<Integer> values;
    private Iterator<Integer> iterator;

    public BSTIterator(TreeNode root) {
        values = new ArrayList<>();
        visit(root, values);
        iterator = values.iterator();
    }

    public int next() {
        return iterator.next();
    }

    public boolean hasNext() {
        return iterator.hasNext();
    }

    private void visit(TreeNode node, List<Integer> values) {
        if (node == null) {
            return;
        }
        visit(node.left, values);
        values.add(node.val);
        visit(node.right, values);
    }


    public static void main(String[] args) {
        TreeNode root = new TreeNode(10);
        root.left = new TreeNode(5);
        root.right = new TreeNode(15);
        BSTIterator bstIterator = new BSTIterator(root);
        System.out.println(bstIterator.next());
        System.out.println(bstIterator.next());
        System.out.println(bstIterator.next());
    }
}
